import { createClock, createNullEntity } from '$lib';
import { fromTemplate } from '$lib/builder';
import { createWorld } from '$lib/ecs';
import { placeOnMap } from '$lib/pos3';
import { randomSeed } from '$lib/random';
import { randomGround, randomUnderground } from './mapgen';
import { moleTemplate } from './mole';
import { deployShip } from './ship';
import { mapHeight, mapWidth } from './size';

randomSeed(123);

export const addPc = (world, x, y, z) => {
  const pc = fromTemplate(world, moleTemplate);
  placeOnMap(world, pc, x, y, z);
  return pc;
};

export const prepareMap = (world) => {
  createNullEntity(world);
  createClock(world);

  randomGround(world, 0);
  randomUnderground(world, -1);
  randomUnderground(world, -2);

  addPc(world, Math.floor(mapWidth/2), Math.floor(mapHeight/2), 0);

  deployShip(world, mapWidth - 8, Math.floor(mapHeight / 2), 0);
};

export const prepareWorld = () => {
  const world = createWorld();
  prepareMap(world);
  return world;
};
